package com.momo.seckilling.service;

import com.momo.seckilling.dao.SeckillProductDao;
import com.momo.seckilling.entity.ProductInfo;
import com.momo.seckilling.entity.SeckillProductInfo;
import com.momo.seckilling.form.SeckillForm;
import com.momo.seckilling.utils.DateUtils;
import com.momo.seckilling.vo.SeckillProductInfoCondition;
import com.momo.seckilling.vo.SeckillProductInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by li on 2019/3/9.
 */
@Service
public class SeckillProductService {
    @Autowired
    private SeckillProductDao seckillProductDao;
    @Autowired
    private ProductInfoService productInfoService;

    public void saveSeckillProduct(SeckillForm seckillForm){
        SeckillProductInfo seckillProductInfo = new SeckillProductInfo();
        if(seckillForm != null){
            seckillProductInfo.setProductid(seckillForm.getProductid());
            seckillProductInfo.setSeckillnum(0);
            seckillProductInfo.setSeckillprice(seckillForm.getSeckillPrice());
            ProductInfo productInfo = productInfoService.findProductById(seckillForm.getProductid());
            seckillProductInfo.setSeckillInventory(productInfo.getProductInventory());
            seckillProductInfo.setCreatetime(new Date());
            String formatString = "yyyy-MM-dd hh:mm:ss";
            Date startTime = DateUtils.tranferTimeBy(seckillForm.getStartTime(),formatString);
            Date endTime = DateUtils.tranferTimeBy(seckillForm.getEndTime(),formatString);
            seckillProductInfo.setStarttime(startTime);
            seckillProductInfo.setEndtime(endTime);
            seckillProductInfo.setShopid(productInfo.getShopId());
            seckillProductInfo.setState(0);
            seckillProductInfo.setProductName(seckillForm.getProductName());
            seckillProductInfo.setProductTitle(seckillForm.getProductTitle());
            seckillProductInfo.setProductprice(seckillForm.getProductPrice());
        }
        seckillProductDao.saveSeckillProduct(seckillProductInfo);
    }

    public List<SeckillProductInfo> listSeckillInfo(SeckillForm seckillForm){
        SeckillProductInfoVo seckillProductInfoVo = new SeckillProductInfoVo();//
        if(seckillForm != null){
                //加上条件
            SeckillProductInfoCondition seckillProductInfoCondition = new SeckillProductInfoCondition();
            if(seckillForm.getShopid() != -1){
                seckillProductInfoCondition.setShopid(seckillForm.getShopid());
            }
            if(seckillForm.getState() != -1){
                seckillProductInfoCondition.setState(seckillForm.getState());
            }
            seckillProductInfoVo.setSeckillProductInfoCondition(seckillProductInfoCondition);
        }

        return seckillProductDao.listSeckillInfo(seckillProductInfoVo);
    }

    public void updateSeckillInfoState(int id,int state){
        SeckillProductInfo seckillProductInfo = new SeckillProductInfo();
        seckillProductInfo.setId(id);
        seckillProductInfo.setState(state);
        if(state == 1||state==2){
            seckillProductInfo.setApproveTime(new Date());
        }
        seckillProductDao.updateSeckillInfoState(seckillProductInfo);
    }
}
